package com.factory.iamp.controller;

import com.alibaba.fastjson.JSONObject;
import com.factory.iamp.pojo.Sap;
import com.factory.iamp.pojo.User;
import com.factory.iamp.service.SapService;
import com.factory.iamp.utils.helper.PageHelper;
import com.factory.iamp.utils.token.LoginToken;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/sap")
@Validated
public class SapController {
    @Resource
    SapService sapService;

    @GetMapping("/all/{limit}/{page}")
    @LoginToken
    public PageHelper getAll(
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestAttribute("iampUser") User user
    ) {
        PageHelper result = new PageHelper(sapService.Count(user.getLine()), page, limit);
        result.setData(sapService.ListPaged(result, user.getLine()));
        return result;
    }

    @GetMapping("/get/{order_id}")
    @LoginToken
    public Sap getOne(@PathVariable("order_id") String order_id, @RequestAttribute("iampUser") User user) {
        return sapService.findByOrderId(order_id, user.getLine());
    }

    @GetMapping("/find/{limit}/{page}")
    @LoginToken
    public PageHelper findByStr(
            @PathVariable("limit") int limit, @PathVariable("page") int page,
            @RequestParam("findstr") String findstr, @RequestAttribute("iampUser") User user
    ) {
        PageHelper result = new PageHelper(sapService.findCount(findstr, user.getLine()), page, limit);
        result.setData(sapService.findByStr(result, findstr, user.getLine()));
        return result;
    }

    @PostMapping("/add")
    public void addOne(@RequestBody JSONObject data) throws Throwable {
//        sapService.addSap(-1, data.getString("data"));
    }

    @DeleteMapping("/delete/{order_id}")
    @LoginToken
    public void deleteOne(@PathVariable("order_id") String order_id, @RequestAttribute("iampUser") User user) {
        sapService.delete(order_id, user.getLine());
    }
}
